import pygame
import random

# 初始化pygame
pygame.init()

# 屏幕尺寸
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("立体射击游戏")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREY = (128, 128, 128)

# 加载字体
font = pygame.font.SysFont(None, 48)

# 玩家设置
player_width = 40
player_height = 60
player_pos = [SCREEN_WIDTH // 2, SCREEN_HEIGHT - player_height - 100]  # 初始位置
player_speed = 7
player_jump_speed = 15
player_velocity_y = 0
is_jumping = False
gravity = 0.8

# 子弹设置
bullet_size = 10
bullet_speed = 15
bullets = []

# 敌人设置
enemy_width = 40
enemy_height = 60
enemy_speed = 3
enemies = []

# 得分
score = 0

def draw_player(x, y):
    pygame.draw.rect(screen, BLUE, (x, y, player_width, player_height))

def draw_bullet(x, y):
    pygame.draw.rect(screen, RED, (x, y, bullet_size, bullet_size))

def draw_enemy(x, y):
    pygame.draw.rect(screen, BLACK, (x, y, enemy_width, enemy_height))

def display_score(score):
    text = font.render(f"得分: {score}", True, BLACK)
    screen.blit(text, (10, 10))

def game_loop():
    global score, bullets, enemies, player_velocity_y, is_jumping
    clock = pygame.time.Clock()
    running = True
    enemy_spawn_timer = 0

    while running:
        screen.fill(WHITE)

        # 绘制地面
        pygame.draw.rect(screen, GREY, (0, SCREEN_HEIGHT - 100, SCREEN_WIDTH, 100))

        enemy_spawn_timer += 1

        # 生成敌人
        if enemy_spawn_timer > 60:
            enemies.append([random.randint(0, SCREEN_WIDTH - enemy_width), -enemy_height])
            enemy_spawn_timer = 0

        # 事件处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE and not is_jumping:
                    player_velocity_y = -player_jump_speed
                    is_jumping = True

        # 按键检测
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and player_pos[0] > 0:
            player_pos[0] -= player_speed
        if keys[pygame.K_RIGHT] and player_pos[0] < SCREEN_WIDTH - player_width:
            player_pos[0] += player_speed
        if keys[pygame.K_SPACE]:
            bullets.append([player_pos[0] + player_width // 2 - bullet_size // 2, player_pos[1]])

        # 跳跃逻辑
        player_velocity_y += gravity
        player_pos[1] += player_velocity_y

        # 地面碰撞检测
        if player_pos[1] >= SCREEN_HEIGHT - player_height - 100:
            player_pos[1] = SCREEN_HEIGHT - player_height - 100
            player_velocity_y = 0
            is_jumping = False

        # 移动并绘制子弹
        for bullet in bullets[:]:
            bullet[1] -= bullet_speed
            if bullet[1] < 0:
                bullets.remove(bullet)

        # 移动并绘制敌人
        for enemy in enemies[:]:
            enemy[1] += enemy_speed
            if enemy[1] > SCREEN_HEIGHT:
                enemies.remove(enemy)

        # 检测碰撞
        for bullet in bullets[:]:
            bullet_rect = pygame.Rect(bullet[0], bullet[1], bullet_size, bullet_size)
            for enemy in enemies[:]:
                enemy_rect = pygame.Rect(enemy[0], enemy[1], enemy_width, enemy_height)
                if bullet_rect.colliderect(enemy_rect):
                    bullets.remove(bullet)
                    enemies.remove(enemy)
                    score += 1

        # 绘制玩家
        draw_player(player_pos[0], player_pos[1])

        # 绘制所有子弹
        for bullet in bullets:
            draw_bullet(bullet[0], bullet[1])

        # 绘制所有敌人
        for enemy in enemies:
            draw_enemy(enemy[0], enemy[1])

        # 显示得分
        display_score(score)

        # 更新屏幕
        pygame.display.flip()
        clock.tick(60)

    pygame.quit()

if __name__ == "__main__":
    game_loop()